//
//  MeViewController.m
//  PT
//
//  Created by jarze on 15/6/10.
//  Copyright (c) 2015年 jarze. All rights reserved.
//

#import "MeViewController.h"
#import "MeTableViewCell.h"
#import "ContactUsViewController.h"
#import "MyBillViewController.h"
#import "MyMessageViewController.h"
#import "NextItem.h"
#import "LabelItem.h"
#import "ContactUs.h"

@interface MeViewController ()

@end

@implementation MeViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationItem.title = @"My Profile";
    self.view.backgroundColor = PTColor(245, 245, 245, 1);
    self.tableView.frame = CGRectMake(0, 64, 320, 200);
    [self.tableView setScrollEnabled:NO];
    [self addGroup];
    
    UIButton *logoutButton = [[UIButton alloc]initWithFrame:CGRectMake(15, 300, 290, 44)];
    [logoutButton setTitle:@"Log out" forState:UIControlStateNormal];
    logoutButton.titleLabel.font = PTHelveticaNeueLight14;
    [logoutButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [logoutButton setBackgroundImage:[UIImage imageNamed:@"Logout"] forState:UIControlStateNormal];
    [self.view addSubview:logoutButton];
    
    
}


- (void)addGroup
{
    LabelItem *item1 = [[LabelItem alloc] initWithIcon:@"Remaining Courses" title:@"Remaining Courses" subTitle:nil label:@"10 Lessons"];
    MeItem *item2 = [[NextItem alloc] initWithIcon:@"My News" title:@"My News" subTitle:nil destClass:[MyMessageViewController class]];
    MeItem *item3 = [[NextItem alloc] initWithIcon:@"My Account" title:@"My Account"  subTitle:nil destClass:[MyBillViewController class]];
    MeItem *item4 = [[NextItem alloc] initWithIcon:@"Recharge" title:@"Recharge"  subTitle:nil];
    MeItem *item5 = [[NextItem alloc] initWithIcon:@"Contact Us" title:@"Contact Us"subTitle:nil  destClass:[ContactUsViewController class]];
    
    
    //  跳转网页
    item4.option = ^{
        NSString *str = [NSString stringWithFormat:@"http://weibo.com"];
            
        NSURL *url  = [NSURL URLWithString:str];
            [[UIApplication sharedApplication] openURL:url];
    };
    
    
    
    
    Group *group =[[Group alloc] init];
    group.items = [NSMutableArray array];
    [group.items addObjectsFromArray:@[item1, item2, item3, item4, item5]];
    [self.datas addObject:group];
   
    
}

- (id)init
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (id)initWithStyle:(UITableViewStyle)style
{
    return [super initWithStyle:UITableViewStyleGrouped];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    Group *g = self.datas[0];
    return g.items.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    MeTableViewCell *cell = [MeTableViewCell cellWithTableView:self.tableView];
    Group *g = self.datas[0];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //  从组模型中取出对应行的模型
    cell.meItem = g.items[indexPath.section];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 立即取消选中
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 先取出对应组的组模型
    Group *g = self.datas[0];
    //  从组模型中取出对应行的模型
    MeItem *item = g.items[indexPath.section];
    // 判断block中是否保存了代码
    //    if (item.option != nil) {
    //        // 如果保存,就执行block中保存的代码
    //        item.option();
    //    }else
    if (item.option != nil) {
        // 如果保存,就执行block中保存的代码
        item.option();
    }
    else if ([item isKindOfClass:[NextItem class]]) {
        // 创建目标控制并且添加到栈中
               NextItem *Item = (NextItem *)item;
               UIViewController *vc = [[Item.destVC alloc] init];
               vc.title = Item.teacherName;
               [self.navigationController pushViewController:vc animated:YES];
    }
   

}





@end
